package ar.com.ford.it.fullEconomics.view.form;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import org.apache.struts.validator.ValidatorForm;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesr01Request;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesr02Requestitm;
import ar.com.ford.it.fullEconomics.util.FullEconomicsDateUtil;

public class RequestDetailForm extends ValidatorForm {

    private String section;
    private String selectedRowNum;
    private String supplierCode;
    private String supplierClass;
    private String operation;
    private String commodity;
    private String partClass;
    private String prefix;
    private String base;
    private String suffix;
    private String percentIncrease;
    private String newPrice;
    private String currencyCode;
    private String effectiveDate;
    private String econLevel;
    private String requestScope;
    private String requestClass;
    private String comments;
    private String vehicleLine;
    private String status;
    private Teesr01Request request;
    private Teesr02Requestitm itemSelected;
    private Collection<Teesr02Requestitm> items;

    public void resetForm() {
        this.section = null;
        this.supplierCode = null;
        this.supplierClass = "-1";
        this.commodity = "-1";
        this.partClass = "-1";
        this.vehicleLine = "-1";
        this.status = "P";
        this.prefix = null;
        this.base = null;
        this.suffix = null;
        this.percentIncrease = null;
        this.newPrice = null;
        this.currencyCode = "-1";
        this.effectiveDate = null;
        this.econLevel = null;
        this.requestScope = null;
        this.requestClass = null;

    }

    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {

        ActionErrors ae = new ActionErrors();
        MessageResources resources = (MessageResources) request
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);
        if (this.getSection().equalsIgnoreCase("create")) {
            if (StringUtils.isBlank(this.getRequestScope())) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "requestDetail.error.invalidRequestScope"));
            } else if (this.getRequestScope().equalsIgnoreCase("commodity")) {
                if (this.getCommodity().equalsIgnoreCase("-1")) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "requestDetail.error.invalidCommodity"));
                }
            } else if (this.getRequestScope().equalsIgnoreCase("part")) {
                if (this.getPartClass().equalsIgnoreCase("-1")) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "requestDetail.error.invalidPart"));
                }
                if (StringUtils.isBlank(this.getBase())) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "requestDetail.error.invalidPart"));
                }
            }
            if (StringUtils.isBlank(this.getRequestClass())) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "requestDetail.error.invalidRequestClass"));
            } else if (this.getRequestClass().equalsIgnoreCase("I")) {
                try {

                    if (Double.valueOf(this.getPercentIncrease()) == null)
                        ae
                                .add(
                                        ActionMessages.GLOBAL_MESSAGE,
                                        new ActionMessage(
                                                "requestDetail.error.invalidPercentIncrease"));
                } catch (Exception e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "requestDetail.error.invalidPercentIncrease"));
                }
            } else if (this.getRequestClass().equalsIgnoreCase("P")) {
                try {
                    if (Double.valueOf(this.getNewPrice()) <= 0)
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage(
                                        "requestDetail.error.invalidNewPrice"));
                } catch (Exception e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "requestDetail.error.invalidNewPrice"));
                }
                if (this.getCurrencyCode().equalsIgnoreCase("-1")) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "requestDetail.error.invalidCurrency"));
                }
            }
            try {
                if (FullEconomicsDateUtil.format(this.getEconLevel()) == null) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "errors.date",
                                            resources
                                                    .getMessage("requestDetail.label.economicLevel")));
                }
            } catch (Exception e) {
                ae
                        .add(
                                ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage(
                                        "errors.date",
                                        resources
                                                .getMessage("requestDetail.label.economicLevel")));
            }
            if (!StringUtils.isBlank(this.getEffectiveDate())) {
                try {
                    if (FullEconomicsDateUtil.format(this.getEffectiveDate()) == null) {
                        ae
                                .add(
                                        ActionMessages.GLOBAL_MESSAGE,
                                        new ActionMessage(
                                                "errors.date",
                                                resources
                                                        .getMessage("requestDetail.label.effectiveDate")));
                    }
                } catch (Exception e) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "errors.date",
                                            resources
                                                    .getMessage("requestDetail.label.effectiveDate")));
                }
            }
        }
        if (this.getSection().equalsIgnoreCase("update")) {
            if (StringUtils.isBlank(this.getRequestClass())) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "requestDetail.error.invalidRequestClass"));
            } else if (this.getRequestClass().equalsIgnoreCase("I")) {
                try {

                    if (Double.valueOf(this.getPercentIncrease()) <= 0)
                        ae
                                .add(
                                        ActionMessages.GLOBAL_MESSAGE,
                                        new ActionMessage(
                                                "requestDetail.error.invalidPercentIncrease"));
                } catch (Exception e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "requestDetail.error.invalidPercentIncrease"));
                }
            } else if (this.getRequestClass().equalsIgnoreCase("P")) {
                try {
                    if (Double.valueOf(this.getNewPrice()) <= 0)
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage(
                                        "requestDetail.error.invalidNewPrice"));
                } catch (Exception e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "requestDetail.error.invalidNewPrice"));
                }
                if (this.getCurrencyCode().equalsIgnoreCase("-1")) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "requestDetail.error.invalidCurrency"));
                }
            }
            try {
                if (FullEconomicsDateUtil.format(this.getEconLevel()) == null) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "errors.date",
                                            resources
                                                    .getMessage("requestDetail.label.economicLevel")));
                }
            } catch (Exception e) {
                ae
                        .add(
                                ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage(
                                        "errors.date",
                                        resources
                                                .getMessage("requestDetail.label.economicLevel")));
            }
            try {
                if (FullEconomicsDateUtil.format(this.getEffectiveDate()) == null) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "errors.date",
                                            resources
                                                    .getMessage("requestDetail.label.effectiveDate")));
                }
            } catch (Exception e) {
                ae
                        .add(
                                ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage(
                                        "errors.date",
                                        resources
                                                .getMessage("requestDetail.label.effectiveDate")));
            }

        }
        return ae;
    }

    public Teesr02Requestitm getRequestItemForCreate() {
        Teesr02Requestitm item = new Teesr02Requestitm();
        item.setTeesr01Request(this.getRequest());
        if (this.getRequestClass().equalsIgnoreCase("I")) {
            item.setEesr02ReqitmIncrPercentC(new BigDecimal(this
                    .getPercentIncrease()));
        } else {
            item.setEesr02ReqitmNewpriceAmounA(new BigDecimal(this
                    .getNewPrice()));
            item.setEesr02CurrencyCodeC(this.getCurrencyCode());
        }
        item.setEesr02ReqitmEcolevelDateY(new Timestamp(FullEconomicsDateUtil
                .format(this.getEconLevel()).getTimeInMillis()));
        if (!StringUtils.isBlank(this.getEffectiveDate())) {
            item.setEesr02ReqitmEffectiveDateY(new Timestamp(
                    FullEconomicsDateUtil.format(this.getEffectiveDate())
                            .getTimeInMillis()));
        } else {
            item.setEesr02ReqitmEffectiveDateY(item
                    .getEesr02ReqitmEcolevelDateY());
        }
        item.setEesr02ReqitmStatusCodeC(this.getStatus());
        return item;
    }

    public Teesr02Requestitm getRequestItemForUpdate() {
        Teesr02Requestitm item = new Teesr02Requestitm();
        item.setTeesr01Request(this.getRequest());
        item.setEesr02ReqitmNumberK(this.getItemSelected()
                .getEesr02ReqitmNumberK());
        item.setEesr02ReqitmStatusCodeC(this.getStatus());
        if (this.getRequestClass().equalsIgnoreCase("I")) {
            item.setEesr02ReqitmIncrPercentC(new BigDecimal(this
                    .getPercentIncrease()));
        } else {
            item.setEesr02CurrencyCodeC(this.getCurrencyCode());
            item.setEesr02ReqitmNewpriceAmounA(new BigDecimal(this
                    .getNewPrice()));
        }
        item.setEesr02ReqitmEcolevelDateY(new Timestamp(FullEconomicsDateUtil
                .format(this.getEconLevel()).getTimeInMillis()));
        item.setEesr02ReqitmEffectiveDateY(new Timestamp(FullEconomicsDateUtil
                .format(this.getEffectiveDate()).getTimeInMillis()));
        return item;
    }

    public void setItemMapped() {
        List items = (List) this.getItems();

        this.setItemSelected((Teesr02Requestitm) items.get(Integer
                .parseInt(this.getSelectedRowNum()) - 1));
    }

    public void setItemForEdit(Teesr02Requestitm item) {
        if (item.getTeesp04Supply() != null) {
            this.setPartClass(item.getTeesp04Supply().getTeesp01Part()
                    .getEesp01PartClassCodeC());
            this.setPrefix(item.getTeesp04Supply().getTeesp01Part()
                    .getEesp01PartPrefixCodeC());
            this.setBase(item.getTeesp04Supply().getTeesp01Part()
                    .getEesp01PartBaseCodeC());
            this.setSuffix(item.getTeesp04Supply().getTeesp01Part()
                    .getEesp01PartSuffixCodeC());
            this.setRequestScope("part");
        } else if (item.getTeesp02Commodity() != null) {
            this.setCommodity(item.getTeesp02Commodity()
                    .getEesp02CommodityNumberK().toString());
            this.setRequestScope("commodity");
        } else
            this.setRequestScope("allParts");
        if (item.getEesr02ReqitmNewpriceAmounA() != null) {
            this.setNewPrice(item.getEesr02ReqitmNewpriceAmounA().toString());
            this.setCurrencyCode(item.getEesr02CurrencyCodeC());
            this.setRequestClass("P");
        } else if (item.getEesr02ReqitmIncrPercentC() != null) {
            this.setPercentIncrease(item.getEesr02ReqitmIncrPercentC()
                    .toString());
            this.setRequestClass("I");
        }
        if (item.getNcas012VehicleVw() == null)
            this.setVehicleLine("-1");
        else
            this.setVehicleLine(item.getNcas012VehicleVw()
                    .getCas012VehicleCodeC());
        this.setStatus(item.getEesr02ReqitmStatusCodeC());
        Calendar calEcoLevel = Calendar.getInstance();
        Calendar calEffDate = Calendar.getInstance();
        calEcoLevel.setTimeInMillis(item.getEesr02ReqitmEcolevelDateY()
                .getTime());
        calEffDate.setTimeInMillis(item.getEesr02ReqitmEffectiveDateY()
                .getTime());
        this.setEconLevel(FullEconomicsDateUtil.format(calEcoLevel));
        this.setEffectiveDate(FullEconomicsDateUtil.format(calEffDate));
    }

    public String getCurrencyCode() {
        return currencyCode;
    }

    public void setCurrencyCode(String currencyCode) {
        this.currencyCode = currencyCode;
    }

    public String getEconLevel() {
        return econLevel;
    }

    public void setEconLevel(String econLevel) {
        this.econLevel = econLevel;
    }

    public String getEffectiveDate() {
        return effectiveDate;
    }

    public void setEffectiveDate(String effectiveDate) {
        this.effectiveDate = effectiveDate;
    }

    public Collection<Teesr02Requestitm> getItems() {
        return items;
    }

    public String getNewPrice() {
        return newPrice;
    }

    public void setNewPrice(String newPrice) {
        this.newPrice = newPrice;
    }

    public String getPercentIncrease() {
        return percentIncrease;
    }

    public void setPercentIncrease(String percentIncrease) {
        this.percentIncrease = percentIncrease;
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public String getBase() {
        return base;
    }

    public void setBase(String base) {
        this.base = base;
    }

    public String getCommodity() {
        return commodity;
    }

    public void setCommodity(String commodity) {
        this.commodity = commodity;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }

    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }

    public String getRequestClass() {
        return requestClass;
    }

    public void setRequestClass(String requestClass) {
        this.requestClass = requestClass;
    }

    public String getRequestScope() {
        return requestScope;
    }

    public void setRequestScope(String requestScope) {
        this.requestScope = requestScope;
    }

    public Teesr02Requestitm getItemSelected() {
        return itemSelected;
    }

    public void setItemSelected(Teesr02Requestitm itemSelected) {
        this.itemSelected = itemSelected;
    }

    public void setItems(Collection<Teesr02Requestitm> items) {
        this.items = items;
    }

    public Teesr01Request getRequest() {
        return request;
    }

    public void setRequest(Teesr01Request request) {
        this.request = request;
    }

    public String getSupplierCode() {
        return supplierCode;
    }

    public void setSupplierCode(String supplierCode) {
        this.supplierCode = supplierCode;
    }

    public String getPartClass() {
        return partClass;
    }

    public void setPartClass(String partClass) {
        this.partClass = partClass;
    }

    public String getSupplierClass() {
        return supplierClass;
    }

    public void setSupplierClass(String supplierClass) {
        this.supplierClass = supplierClass;
    }

    public String getSelectedRowNum() {
        return selectedRowNum;
    }

    public void setSelectedRowNum(String selectedRowNum) {
        this.selectedRowNum = selectedRowNum;
    }

    public String getComments() {
        return comments;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getVehicleLine() {
        return vehicleLine;
    }

    public void setVehicleLine(String vehicleLine) {
        this.vehicleLine = vehicleLine;
    }
}
